/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import email.Email;
import core.User;
import db.factories.UserDAOFactory;
import db.factories.UserDAOFactory.DAOTYPE;
import db.interfaces.UserDAO;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.validator.ValidatorException;
import javax.mail.MessagingException;


/**
 *
 * @author Wizz
 */
public class RegisterBean {


    private User registerUser;
    private String  confirmPassword, password = null, email;
    private boolean registerComplete, registerCompleteTwo;
    private UserDAO dao;

    /** Creates a new instance of RegisterBean */
    public RegisterBean()
    {
        registerUser = new User();
        dao = UserDAOFactory.getDAO(DAOTYPE.JDBC);
        registerComplete = true;
        registerCompleteTwo = false;

    }

    public boolean getRegisterComplete()
    {
        return registerComplete;
    }

    public void setRegisterComplete(boolean registerComplete)
    {
        this.registerComplete = registerComplete;
    }

    public boolean getRegisterCompleteTwo()
    {
        return registerCompleteTwo;
    }

    public void setRegisterCompleteTwo(boolean registerCompleteTwo)
    {
        this.registerCompleteTwo = registerCompleteTwo;
    }

    public void enableRegister(ActionEvent e)
    {
        this.registerComplete = true;
        this.registerCompleteTwo = false;
    }

    public User getUser()
    {
        return registerUser;
    }

    public void setUser(User user)
    {
        this.registerUser = user;
    }

    public String getConfirmPassword()
    {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword)
    {
        this.confirmPassword = confirmPassword;
    }

    public void validateRegisterEmail(FacesContext context, UIComponent validate, Object value) throws ValidatorException{

            email = (String) value;

            if(email.indexOf('@')==-1){
            ((UIInput)validate).setValid(false);
            FacesMessage msg = new FacesMessage("Invalid Email");
            context.addMessage(validate.getClientId(context), msg);
            }
            else if(dao.containsUser(email)){
                ((UIInput)validate).setValid(false);
                FacesMessage msg = new FacesMessage("User already exist");
                context.addMessage(validate.getClientId(context), msg);
            }
  
    }
    public void validateRegisterPasswords(FacesContext context, UIComponent validate, Object value) throws ValidatorException{

        if(password == null){
            password = (String) value;
            return;
        }
        if(!password.equals((String)value)){
                password = null;
                ((UIInput)validate).setValid(false);
                FacesMessage msg = new FacesMessage("Passwords don't match!");
                context.addMessage(validate.getClientId(context), msg);
        }

    }

    public void submitRegister(ActionEvent e)  throws MessagingException
    {

        dao.addUser(registerUser);
        if(dao.containsUser(registerUser)){
            registerCompleteTwo = true;
            registerComplete = false;
        }
        User u = dao.getUserByEmail(email);
        sendRegisterEmail(u);
        System.out.println("submitRegister ");
    }

    private void sendRegisterEmail(User u) throws MessagingException
    {
        String subject = "Welcome to www.bookSelector.se!";
        String message =  "We at the bookSelector welcome you " + u.getFname() + " " + u.getLname() + " to our website!" + "\n" +
                           "We hope you have much use of the bookSelector." + "\n" +  "\n" +
                           "Here is your user information at www.bookSelector.se:" + "\n" +
                           "Username: " + u.getEmail() + "\n" +
                           "Password: " + u.getPassword() + "\n" + "\n" +
                           "Regards"+ "\n"+ "\n"+
                           "The bookSelector Crew";

        Email.sendMail(u.getEmail(), subject, message);
    }
}

